﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using DynaSpace.Entity;

namespace DynaSpace.SampleEntity
{
	public class SampleEntityComponent : DynaSpace.DynaComponentBase
	{
		public SampleEntityComponent(DynaGame game)
			: base(game)
		{ }

		// -- textury --
		internal static Texture2D texGround, texBall;

		// -- objekty hry --
		private PlayerView view, view2;
		Player player; 

		public override void Initialize()
		{
			// vytvořit mapu a vygenerovat entity
			Random r = new Random();
			Game.Playground = new Playground(100, 50);
			for (int x = 0; x < 200; x++)
			{
				// policka zeme
				Game.Playground.Map.Add(new GroundEntity(Game.Playground, new Vector2(x, 21)));

				// policka vln
				Game.Playground.Map.Add(new GroundEntity(Game.Playground, new Vector2(x, MathHelper.FloatSinus((float)x / 3f, 14, 20))));

				// policka vln na stridacku
				if(x % 2 == 0)
					Game.Playground.Map.Add(new GroundEntity(Game.Playground, new Vector2(x, MathHelper.FloatSinus((float)x / 3f, 13, 19))));

			}

			// přidat hráče
			player = new Player(Game.Playground, new Vector2(5, 0), PlayerIndex.One);
			
			// pohánět fyziku hráče enginem pro ovládání hráče
			player.Engine = new PlayerEngine(Game.Playground);

			// přidat na mapu
			Game.Playground.Map.Add(player);

			// vytvořit pohledy (2 poloviny pro split screen)
			int w = Game.GraphicsDevice.PresentationParameters.BackBufferWidth;
			int h = Game.GraphicsDevice.PresentationParameters.BackBufferHeight;
			view = new PlayerView(0, 0, w / 2, h, null, Game.Playground, PlayerView.ScrollingSpeed.Fixed); // jeden pohled je fixni
			view2 = new PlayerView(w / 2, 0, w / 2, h, player, Game.Playground, PlayerView.ScrollingSpeed.SlowFollow); // druhy s pomalym nasledovanim
		
			base.Initialize();
		}

		protected override void LoadContent()
		{
			base.LoadContent();

			// načíst textury
			texGround = Game.Content.Load<Texture2D>("ground");
			texBall = Game.Content.Load<Texture2D>("ball");
		}

		public override void Update(GameTime gameTime)
		{
			base.Update(gameTime);

			// vytvořit vektor pozice myši
			MouseState mouse = Mouse.GetState();
			Vector2 followVector = new Vector2(5, 15) + new Vector2(mouse.X, mouse.Y) / Entity.EntityBase.SIZE;

			// pokud stiskne hrac 1 tlacitko akce, tak se vytvori entita bomby na pozici hrace
			if(Game.IsKeyPressedOnce(PlayerIndex.One, DynaSpace.Input.PlayerButton.Action))
			{
				Playground.Map.Add(new BallEntity(Playground, player.Position));
			}

			// nechat pohled sledovat vektor a druhý míč
			view.Update(gameTime, followVector);
			view2.Update(gameTime);
		}

		public override void Draw(GameTime gameTime)
		{
			base.Draw(gameTime);

			// vykreslit pohledy
			view.Render(spriteBatch); // levy pruh
			view2.Render(spriteBatch); // pravy pruh
		}
	}
}
